window.onload = function() {
	document.getElementById('addRessourceLink').addEventListener('click', function (event){openFrm()}, false)
	var lstInput = document.getElementsByTagName('input');
	for (i=0; i<lstInput.length; i++) {
		if (lstInput[i].type == 'reset') {
			lstInput[i].addEventListener('click', function(event) {
				document.getElementById('addRessource').style.display='none';
			}, false)
		}
	}
	var lstImg = document.getElementsByTagName('img');
	for (i = 0; i < lstImg.length; i++) {
		if(lstImg[i].id.indexOf('delete') == 0) {
			lstImg[i].addEventListener('click', function (event) {deleteRes(event.target)}, false);
		}
		if(lstImg[i].id.indexOf('edit') == 0) {
			lstImg[i].addEventListener('click', function (event) {editRes(event.target)}, false);
		}
	}
}

function openFrm() {
	document.getElementById('addRessource').style.display='block';
	document.getElementsByName('frm_id')[0].value = '';
}

function deleteRes(target) {
	var id = target.id.substr(target.id.indexOf('_')+1);
	var cible = '/Clan/deleteRessource/'+id;
	execScriptServeur(traiterReponse, cible);
}

function editRes(target) {
	document.getElementById('addRessource').style.display='block';
	document.getElementsByName('frm_nom')[0].value = target.previousElementSibling.textContent;
	document.getElementsByName('frm_lien')[0].value = target.previousElementSibling.href;
	document.getElementsByName('frm_description')[0].value = target.previousElementSibling.title;
	document.getElementsByName('frm_id')[0].value = target.id.substr(target.id.indexOf('_')+1);
}

function traiterReponse(xhr) {
	if (xhr.readyState==4) 
    {   
		var docXML= xhr.responseXML;
		var id = docXML.getElementsByTagName('id')[0].textContent;
		var li = document.getElementById('delete_'+id).parentElement;
		li.parentElement.removeChild(li);
		tempo();
    }
}